<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithTitle; //sheet名称
use Maatwebsite\Excel\Concerns\WithMultipleSheets;//sheet
use Maatwebsite\Excel\Concerns\WithStyles;//造型
use Maatwebsite\Excel\Concerns\WithColumnWidths; //列宽
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

class TeacherExport implements WithHeadings, FromCollection, WithMultipleSheets, WithTitle, WithColumnWidths, WithStyles
{
    use Exportable;

    private $data;//数据
    private $status; //sheet名称（订单状态）
//    private $column; //总行数
//    private $quesNum = []; //单元数量
//    private $childNum = []; //题目数量
    public function __construct($data, $status = '')
    {
        $this->data = $data;
        $this->status = $status;
    }

    /**
     * 表头
     * @return string[]
     */
    public function headings(): array
    {
        return [
            '单元内容', '单元题库', '考生代号', '时间安排'
        ];
    }

    /**
     * 导出
     * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection
     */
    public function collection()
    {
        $data = $this->data;
        $list = [];
//        foreach ($data as $key => $value) {
            foreach ($data['children'] as $k => $item) {
//                $this->quesNum[] = $item['count'];
                foreach($item['children'] as $k1 => $v1){
//                    $this->childNum[] = $v1['count'];
                     foreach($v1['students'] as $k2 => $v2){
                        $list[] = [
                            'ques_name'        =>$item['stem'],
                            'child_name'       => $v1['name'],
                            'alias'      =>    $v2['alias'],
                            'start_time'         => $v2['start_time']
                        ];
                    }
                }
            }
//        }
//        $this->column = 1;

        return collect($list);
    }

    /**
     * 创建sheet
     * @return array
     */
    public function sheets(): array
    {
        $list = $this->data;
        $sheets = [];
        foreach ($list as $key => $value) {
            $sheets[] = new TeacherExport($list[$key], $value['name']);
        }

        return $sheets;
    }

    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return $this->status;
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 40, 'B' => 40, 'C' => 40, 'D' => 40, 'E' => 50
        ];
    }

//    /**
//     * 格式化列
//     * @return array
//     */
//    public function columnFormats(): array
//    {
//        $format = NumberFormat::FORMAT_NUMBER_00;//金额保留两位小数
//        return ['G' => $format, 'H' => $format, 'I' => $format, 'J' => $format, 'K' => $format];
//    }

    /**
     * 样式设置
     * @param Worksheet $sheet
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     */
    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
        $sheet->getStyle('A1:Z1265')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1:Z1265')->applyFromArray(['alignment' => ['horizontal' => 'center']]);//设置水平居中
//        $sheet->getStyle('F1:K' . $this->column)->applyFromArray(['alignment' => ['horizontal' => 'center']]);//设置水平居中
        $sheet->getStyle('A1:Z1')->applyFromArray(['font' => ['bold' => true, 'color' => ['rgb' => '0072ff']]]);//字体设置
//        $cell = ['A', 'B'];//需要合并的单元格
        //$sheet->mergeCells('A18:A22'); //合并单元格
//        foreach ($cell as $item) {
//            $start = 1;
//            $start1 = 1;
//            foreach ($this->quesNum as $key => $value) {
//                $end = $start + $value;
//                $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
//                $start = $end + 1;
//            }
//            foreach ($this->childNum as $key1 => $value1) {
//                $end1 = $start1 + $value1;
//                $sheet->mergeCells($item . $start1 . ':' . $item . $end1); //合并单元格
//                $start1 = $end1 + 1;
//            }
//        }
    }
}
